package cn.smxy.admin.service.impl;

import cn.smxy.admin.service.UserService;
import cn.smxy.client.UserClient;
import cn.smxy.excel.UserExcel;
import cn.smxy.param.CampusRemoveParam;
import cn.smxy.param.CampusSearchParam;
import cn.smxy.param.PageParam;
import cn.smxy.param.UserSearchParam;
import cn.smxy.pojo.Campus;
import cn.smxy.pojo.User;
import cn.smxy.util.R;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.util.MapUtils;
import com.alibaba.excel.write.style.column.LongestMatchColumnWidthStyleStrategy;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.Map;

@Service
@Slf4j
public class UserServiceImpl implements UserService {

    @Autowired
    private UserClient userClient;

    /**
     * 删除用户
     *
     * @param userId
     * @return
     */
    @Override
    public R delete(Long userId) {
        R r = userClient.delete(userId);
        log.info("UserServiceImpl.delete业务结束，结果为:{}",r);
        return r;
    }

    /**
     * 修改用户信息
     *
     * @param user
     * @return
     */
    @Override
    public R update(User user) {
        return userClient.update(user);
    }

    /**
     * 分页展示用户列表
     *
     * @param pageParam
     * @return
     */
    @Override
    public R page(PageParam pageParam) {
        R r = userClient.page(pageParam);
        log.info("UserServiceImpl.page业务结束，结果为:{}",r);
        return r;
    }

    /**
     * 用户信息详情
     *
     * @param userId
     * @return
     */
    @Override
    public User basic(Long userId) {
        return userClient.basic(userId);
    }

    /**
     * 模糊分页查询用户
     *
     * @param userSearchParam
     * @return
     */
    @Override
    public R search(UserSearchParam userSearchParam) {
        return userClient.search(userSearchParam);
    }

    /**
     * 校园认证批量删除
     *
     * @param campusRemoveParam 校园认证ID集合 用户ID集合
     * @return
     */
    @Override
    public R campusRemoveBatch(CampusRemoveParam campusRemoveParam) {
        return userClient.campusRemoveBatch(campusRemoveParam);
    }

    /**
     * 校园学生认证
     *
     * @param campus
     * @return
     */
    @Override
    public R check(Campus campus) {
        return userClient.check(campus);
    }

    /**
     * 分页展示校园认证
     *
     * @param campusSearchParam
     * @return
     */
    @Override
    public R campusPage(CampusSearchParam campusSearchParam) {
        return userClient.campusPage(campusSearchParam);
    }

    /**
     * excel导出
     *
     * @param response
     */
    @Override
    public void export(HttpServletResponse response) throws IOException {
        try {
            response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
            response.setCharacterEncoding("utf-8");
            String fileName = URLEncoder.encode("用户名单", "UTF-8").replaceAll("\\+", "%20") + System.currentTimeMillis();
            response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
            EasyExcel.write(response.getOutputStream(), UserExcel.class)
                    .registerWriteHandler(new LongestMatchColumnWidthStyleStrategy())
                    .autoCloseStream(Boolean.FALSE)
                    .sheet("sheet1").doWrite(userClient.getAll());
        } catch (Exception e) {
            response.reset();
            response.setContentType("application/json");
            response.setCharacterEncoding("utf-8");
            Map<String, String> map = MapUtils.newHashMap();
            map.put("status", "failure");
            map.put("message", "下载文件失败" + e.getMessage());
            ObjectMapper mapper = new ObjectMapper();
            response.getWriter().println(mapper.writeValueAsString(map));
        }
    }

    /**
     * 图片上传
     *
     * @param img
     * @return
     */
    @Override
    public Object upload(MultipartFile img) throws Exception {
        return userClient.upload(img);
    }

    /**
     * 添加用户
     *
     * @param user
     * @return
     */
    @Override
    public R save(User user) {
        return userClient.register(user);
    }

    /**
     * excel批量导入
     *
     * @param file
     * @return
     * @throws IOException
     */
    @Override
    public R saveExcel(MultipartFile file) throws IOException {
        return userClient.saveExcel(file);
    }
}
